﻿using OvenParameterForm.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OvenParameterForm.Technics
{
    public class 小补气 : TechnicsBaseClass
    {
        public string 充气设定 { get; set; }
        public string 工艺时间 { get; set; }
        private double 当前压力 = 0.0;
        private int 当前计时 = 0;
        private int 总时间;
        private bool 达到小补气;
        private bool 正在小补气;
 
        public 小补气(string[] strings):base(strings)
        {
            充气设定 = strings[1];
            工艺时间 = strings[2];
        }
        public 小补气(string name):base(name) 
        {

        }
       public override void Run()
        {
            当前计时 = 0;
            总时间 = Convert.ToInt32(工艺时间) * 60;
            PLC.WriteData(ovenPLCaddress.抽气地址, "0");
            PLC.WriteData(ovenPLCaddress.小补气地址, "0");
            base.Run();
        }
        protected override void updatetime(object state)
        {
            当前压力 = Convert.ToDouble(PLC.ReadData(ovenPLCaddress.烘箱压力地址));
            正在小补气 = PLC.ReadData(ovenPLCaddress.小补气地址).Equals("1");
            达到小补气 = 当前压力/1000 <= Convert.ToDouble(充气设定);
            switch (PublicFunc.BoolArrayGetValue(new bool[] { 达到小补气, 正在小补气 }))
            {
                case 0:
                    PLC.WriteData(ovenPLCaddress.小补气地址, "1");
                    sendStatus($"开始充气...计时：{当前计时}/{总时间}");
                    break;
                case 1:
                    sendStatus($"正在充气...计时：{当前计时}/{总时间}");
                    break;
                case 2:
                    sendStatus($"充气压力已达标...计时：{当前计时}/{总时间}");
                    break;
                case 3:
                    PLC.WriteData(ovenPLCaddress.小补气地址, "0");
                    sendStatus($"充气压力刚达标，充气停止...计时：{当前计时}/{总时间}");
                    break;
            }

            当前计时++;
        }

        protected override bool Complete()
        {
            if (当前计时 == 总时间)
            {
                if (!达到小补气) PLC.WriteData("没充到报警地址", "1");
                PLC.WriteData(ovenPLCaddress.小补气地址, "0");
                return true;
            }
            else
            {
                return false;
            }
        }

        protected override void ClearVariable()
        {
            PLC.WriteData(ovenPLCaddress.小补气地址, "0");
        }
    }
}
